package GUI;

import com.mysql.cj.jdbc.exceptions.MySQLQueryInterruptedException;
import 数据库和网络.myDatebase;
import 试题和试卷.*;


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;

/*需要对pepercheckingwindow界面进行调整，这个窗口是在组卷中摁显示试卷得到的！！！*/
public class paperCheckingWindowForRandom extends JFrame {

    public static void main(String[] args) {
        new paperCheckingWindow();
    }
    ArrayList<? extends questionAddItem> defList;
    static ArrayList<? extends questionAddItem> selList;
    static ArrayList<? extends question> sel;
    ArrayList<? extends questionAddItem> fillList;
    ArrayList<? extends questionAddItem> subList;
    ArrayList<? extends question> def;
    ArrayList<? extends question> fill;
    ArrayList<? extends question> sub;

    public static JPanel mainPanel;
    static int currentPage;
    static int page;
    static int size=0;
    JLabel num;
    JLabel paperLabel;
    static JLabel pageNumberLabel;
    static JLabel infoLabel;
    static JButton nextButton,preButton,addButton;
    static Font font = new Font("sanserif",Font.BOLD,10);
    Font pageNumberFont = new Font("sanserif",Font.BOLD,12);

    public paperCheckingWindowForRandom(){
        mainPanel = new JPanel();
        JMenuBar menuBar = new JMenuBar();
        Font bigFont = new Font("sanserif",Font.BOLD,24);
        this.setJMenuBar(menuBar);
        //menuBar.setSize(this.getWidth(),30);
        JMenu SelectiveMenu = new JMenu("Selective");
        JMenu DefinitionMenu = new JMenu("Definition");
        JMenu FillingBlankMenu = new JMenu("FillingBlank");
        JMenu SubjectiveMenu = new JMenu("Subjective");
        JMenuItem Selective = new JMenuItem("选择题");
        JMenuItem Definition = new JMenuItem("判断题");
        JMenuItem FillingBlank = new JMenuItem("填空题");
        JMenuItem Subjective = new JMenuItem("简答题");
        JMenu organizeMenu = new JMenu("organize");
        JMenuItem organize = new JMenuItem("组卷");
        JMenu quitMenu = new JMenu("Quit");
        JMenuItem quit = new JMenuItem("退出");


        menuBar.add(SelectiveMenu);
        menuBar.add(DefinitionMenu);
        menuBar.add(FillingBlankMenu);
        menuBar.add(SubjectiveMenu);
        menuBar.add(organizeMenu);
        menuBar.add(quitMenu);

        Selective.addActionListener(new SelectiveListener());
        SelectiveMenu.add(Selective);
        Definition.addActionListener(new DefinitionListener());
        DefinitionMenu.add(Definition);
        FillingBlank.addActionListener(new FillingBlankListener());
        FillingBlankMenu.add(FillingBlank);
        Subjective.addActionListener(new SubjectiveListener());
        SubjectiveMenu.add(Subjective);
        organizeMenu.add(organize);
        organize.addActionListener(new organizeListener());
        quitMenu.add(quit);
        quit.addActionListener(new quitListener());


        mainPanel.setLayout(null);
        this.setTitle("试题库与组卷");
        this.setContentPane(mainPanel);
        this.setSize(980,700);
        mainPanel.setSize(this.getSize());
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        //this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        //this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    }
    public class quitListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            new teacherWindow("a");
            paperCheckingWindowForRandom.this.dispose();
        }
    }
    public class organizeListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            new organizePaperWindow();
        }
    }
    public class SelectiveListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            mainPanel.removeAll();
            currentPage=1;
            sel= randomParameterWindow.sellist;
            selList= randomParameterWindow.selAdds;

            try
            {
                size = sel.size();
            }catch(NullPointerException xe){
                JLabel ex=new JLabel("诶呀，本试卷未添加相应题目~");
                mainPanel.add(ex);
            }
            page=size/5+1;

            if (page == 1) {
                for (int i = 1; i <= size; i++) {
                    add(selList.get(i-1));
                }
            } else if (page > 1) {
                for (int i = (currentPage - 1) * 5; i <= currentPage * 5 - 1; i++) {
                    add(selList.get(i));
                }

            }

            mainPanel.updateUI();


            displayFrame();
            addButton.addActionListener(new addSelectionListener());
            infoLabel.setText("选择题题目数目："+sel.size());
            nextButton.removeAll();
            preButton.removeAll();
            nextButton.addActionListener(new selNextListener());
            preButton.addActionListener(new selPreListener());

        }
    }

    public class DefinitionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            mainPanel.removeAll();
            currentPage = 1;
            def = randomParameterWindow.deflist;
            defList = randomParameterWindow.defAdds;
            try
            {
                size = def.size();
            }catch(NullPointerException xe){
                JLabel ex=new JLabel("诶呀，本试卷未添加相应题目~");
                mainPanel.add(ex);
            }
            page=size/5+1;
            displayFrame();

            if (page == 1) {
                for (int i = 0; i <size; i++) {
                    add(defList.get(i));
                }
            } else if (page > 1) {
                for (int i = (currentPage - 1) * 5; i <= currentPage * 5 - 1; i++) {
                    add(defList.get(i));
                }
            }


            nextButton.addActionListener(new defNextListener());
            preButton.addActionListener(new defPreListener());
            infoLabel.setText("判断题题目数目：" + def.size());
            addButton.addActionListener(new addDefinitionListener());
            mainPanel.updateUI();



        }
    }

    public class FillingBlankListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            mainPanel.removeAll();
            currentPage=1;
            fill=randomParameterWindow.filllist;
            fillList= randomParameterWindow.fillAdds;
            try
            {
                size = fill.size();
            }catch(NullPointerException xe){
                JLabel ex=new JLabel("诶呀，本试卷未添加相应题目~");
                mainPanel.add(ex);
            }
            page=size/5+1;

            if (page == 1) {
                for (int i = 0; i <size; i++) {
                    add(fillList.get(i));
                }
            } else if (page > 1) {
                for (int i = (currentPage - 1) * 5; i <= currentPage * 5 - 1; i++) {
                    add(fillList.get(i));
                }

            }

            mainPanel.updateUI();


            displayFrame();
            infoLabel.setText("填空题题目数目："+fill.size());
            mainPanel.updateUI();
            addButton.addActionListener(new addFillingBlankListener());
            nextButton.removeAll();
            preButton.removeAll();
            nextButton.addActionListener(new fillNextListener());
            preButton.addActionListener(new fillPreListener());
        }
    }

    public class SubjectiveListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            mainPanel.removeAll();
            currentPage=1;
            sub= randomParameterWindow.sublist;
            subList= randomParameterWindow.subAdds;
            try
            {
                size = sub.size();
            }catch(NullPointerException xe){
                JLabel ex=new JLabel("诶呀，本试卷未添加相应题目~");
                mainPanel.add(ex);
            }

            page=size/5+1;

            if (page == 1) {
                for (int i = 0; i < size; i++) {
                    add(subList.get(i));
                }
            } else if (page > 1) {
                for (int i = (currentPage - 1) * 5; i <= currentPage * 5 - 1; i++) {
                    add(subList.get(i));
                }

            }

            mainPanel.updateUI();
            displayFrame();
            infoLabel.setText("简答题题目数目："+sub.size());
            mainPanel.updateUI();
            addButton.addActionListener(new addSubjectiveListener());
            nextButton.removeAll();
            preButton.removeAll();
            nextButton.addActionListener(new subNextListener());
            preButton.addActionListener(new subPreListener());
        }
    }

    public  class addSelectionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            new addSelectionWindow();

        }
    }

    public class addDefinitionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            new addDefinitionWindow();

        }
    }

    public class addFillingBlankListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            new addFillingBlankWindow();

        }
    }

    public class addSubjectiveListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            new addSubjectiveWindow();

        }
    }
    public class selNextListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {

            for(int i= (currentPage-1)*5;i<=currentPage*5-1;i++){
                remove(selList.get(i));
            }
            currentPage++;
            pageNumberLabel.setText(currentPage+"/"+page);
            if(currentPage!=1){
                mainPanel.add(preButton);}
            if(currentPage==page){
                mainPanel.remove(nextButton);
                for(int i= (currentPage-1)*5;i<size;i++){
                    add(selList.get(i));
                }
            }else{
                for(int i= (currentPage-1)*5;i<=(currentPage*5-1);i++){
                    add(selList.get(i));
                }
            }

            mainPanel.updateUI();

        }
    }
    public class selPreListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            for(int i=0;i<size;i++){
                remove(selList.get(i));
            }


            currentPage--;
            if(currentPage==1){
                mainPanel.remove(preButton);
            }
            pageNumberLabel.setText(currentPage+"/"+page);
            if(currentPage!=page){
                mainPanel.add(nextButton);
            }
            for(int i= (currentPage-1)*5;i<=currentPage*5;i++){
                add(selList.get(i));
            }

            if(currentPage<1){
                currentPage=1;
            }
            mainPanel.updateUI();
        }
    }
    public class defNextListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {

            for(int i= (currentPage-1)*5;i<=currentPage*5-1;i++){
                remove(defList.get(i));
            }
            currentPage++;
            pageNumberLabel.setText(currentPage+"/"+page);
            if(currentPage!=1){
                mainPanel.add(preButton);}
            if(currentPage==page){
                mainPanel.remove(nextButton);
                for(int i= (currentPage-1)*5;i<size;i++){
                    add(defList.get(i));
                }
            }else{
                for(int i= (currentPage-1)*5;i<=(currentPage*5-1);i++){
                    add(defList.get(i));
                }
            }

            mainPanel.updateUI();

        }
    }
    public class defPreListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            for(int i=0;i<size;i++){
                remove(defList.get(i));
            }


            currentPage--;
            if(currentPage==1){
                mainPanel.remove(preButton);
            }
            pageNumberLabel.setText(currentPage+"/"+page);
            if(currentPage!=page){
                mainPanel.add(nextButton);
            }
            for(int i= (currentPage-1)*5;i<=currentPage*5;i++){
                add(defList.get(i));
            }

            if(currentPage<1){
                currentPage=1;
            }
            mainPanel.updateUI();
        }
    }
    public class fillNextListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {

            for(int i= (currentPage-1)*5;i<=currentPage*5-1;i++){
                remove(fillList.get(i));
            }
            currentPage++;
            pageNumberLabel.setText(currentPage+"/"+page);
            if(currentPage!=1){
                mainPanel.add(preButton);}
            if(currentPage==page){
                mainPanel.remove(nextButton);
                for(int i= (currentPage-1)*5;i<size;i++){
                    add(fillList.get(i));
                }
            }else{
                for(int i= (currentPage-1)*5;i<=(currentPage*5-1);i++){
                    add(fillList.get(i));
                }
            }

            mainPanel.updateUI();

        }
    }
    public class fillPreListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            for(int i=0;i<size;i++){
                remove(fillList.get(i));
            }


            currentPage--;
            if(currentPage==1){
                mainPanel.remove(preButton);
            }
            pageNumberLabel.setText(currentPage+"/"+page);
            if(currentPage!=page){
                mainPanel.add(nextButton);
            }
            for(int i= (currentPage-1)*5;i<=currentPage*5;i++){
                add(fillList.get(i));
            }

            if(currentPage<1){
                currentPage=1;
            }
            mainPanel.updateUI();
        }
    }
    public class subNextListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {

            for(int i= (currentPage-1)*5;i<=currentPage*5-1;i++){
                remove(subList.get(i));
            }
            currentPage++;
            pageNumberLabel.setText(currentPage+"/"+page);
            if(currentPage!=1){
                mainPanel.add(preButton);}
            if(currentPage==page){
                mainPanel.remove(nextButton);
                for(int i= (currentPage-1)*5;i<size;i++){
                    add(subList.get(i));
                }
            }else{
                for(int i= (currentPage-1)*5;i<=(currentPage*5-1);i++){
                    add(subList.get(i));
                }
            }

            mainPanel.updateUI();

        }
    }
    public class subPreListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            for(int i=0;i<size;i++){
                remove(subList.get(i));
            }


            currentPage--;
            if(currentPage==1){
                mainPanel.remove(preButton);
            }
            pageNumberLabel.setText(currentPage+"/"+page);
            if(currentPage!=page){
                mainPanel.add(nextButton);
            }
            for(int i= (currentPage-1)*5;i<=currentPage*5;i++){
                add(subList.get(i));
            }

            if(currentPage<1){
                currentPage=1;
            }
            mainPanel.updateUI();
        }
    }

    public  void setTitleType(JLabel label) {
        Font bigFont = new Font("sanserif", Font.PLAIN, 16);
        label.setBackground(Color.lightGray);
        label.setOpaque(true);
        label.setFont(bigFont);
        label.setBorder(BorderFactory.createLineBorder(Color.darkGray));
        label.setHorizontalAlignment(SwingConstants.CENTER);
    }

    public void setTextType(JLabel label) {
        Font smallFont = new Font("sanserif", Font.PLAIN, 12);
        label.setBackground(Color.white);
        label.setOpaque(true);
        label.setFont(smallFont);
        label.setBorder(BorderFactory.createLineBorder(Color.lightGray));
        label.setHorizontalAlignment(SwingConstants.CENTER);
    }

    public void setButtonType(JButton button) {
        Font smallFont = new Font("sanserif", Font.PLAIN, 14);
        button.setBackground(Color.GRAY);
        button.setFont(smallFont);
        button.setBorder(BorderFactory.createLineBorder(Color.darkGray));
        button.setHorizontalAlignment(SwingConstants.CENTER);
    }

    public void displayFrame() {
        infoLabel = new JLabel("题目数目:");
        JLabel numLabel = new JLabel("序号");
        JLabel paperInfoLabel = new JLabel("题目信息");
        JLabel paperDetailLabel = new JLabel("修改");
        JLabel paperDoLabel = new JLabel("删除");

        preButton = new JButton("上一页");

        preButton.setBounds(750, 450, 70, 30);
        preButton.setFont(font);
        preButton.setBackground(Color.lightGray);

        addButton = new JButton("添加试题");
        //overallButton.addActionListener(new studentRank.overallListener());
        addButton.setBounds(810, 50, 90, 40);
        //overallButton.setFont(font);
        addButton.setBackground(Color.lightGray);


        setTitleType(numLabel);
        setTitleType(paperInfoLabel);
        setTitleType(paperDetailLabel);
        setTitleType(paperDoLabel);

        JButton backButton = new JButton("返回");

        mainPanel.setLayout(null);

        backButton.setBackground(Color.lightGray);

        infoLabel.setBounds(80, 30, 200, 40);
        numLabel.setBounds(80, 120, 60, 50);
        paperInfoLabel.setBounds(140, 120, 600, 50);
        paperDetailLabel.setBounds(740, 120, 80, 50);
        paperDoLabel.setBounds(820, 120, 80, 50);

        backButton.setBounds(820, 500, 80, 30);
        backButton.addActionListener(new backListener());

        mainPanel.add(paperDetailLabel);
        mainPanel.add(paperDoLabel);
        mainPanel.add(numLabel);
        mainPanel.add(infoLabel);
        mainPanel.add(paperInfoLabel);
        mainPanel.add(backButton);
        mainPanel.add(addButton);


        //size =paperNumber;

        page = (size - 1) / 5 + 1;
        currentPage = 1;

        pageNumberLabel = new JLabel(currentPage + "/" + page);
        mainPanel.add(pageNumberLabel);
        // pageNumberLabel.setFont(pageNumberFont);
        pageNumberLabel.setBounds(430, 430, 80, 30);


        nextButton = new JButton("下一页");

        nextButton.setBounds(830, 450, 70, 30);
        nextButton.setFont(font);
        nextButton.setBackground(Color.lightGray);
        mainPanel.add(nextButton);


        //backButton.addActionListener(new studentRank.backListener());
        infoLabel.setHorizontalAlignment(SwingConstants.CENTER);
        infoLabel.setVerticalAlignment(SwingConstants.CENTER);
        this.setSize(980, 700);
        mainPanel.setSize(this.getSize());
        mainPanel.setBackground(Color.white);
        this.setBackground(Color.white);
        this.setContentPane(mainPanel);
        this.setLocationRelativeTo(null);//居中
        this.setTitle("学生在线考试平台");
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public  void add(questionAddItem item) {
        mainPanel.add(item.num);
        mainPanel.add(item.paperLabel);
        mainPanel.add(item.detailButton);
    }
    public void remove(questionAddItem item) {
        mainPanel.remove(item.num);
        mainPanel.remove(item.paperLabel);
        mainPanel.remove(item.detailButton);
    }

    public class backListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e){
            paperCheckingWindowForRandom.this.dispose();
            new teacherWindow("a");
        }
    }
}

